package com.packtpub.esh;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.StringTokenizer;

/**
* Mapper类。向Reducer提供的是大量的以单词为键、以1为值的键值对
*/
public class WordsMapper extends Mapper<Object, Text, Text, IntWritable> {

	private final static IntWritable one = new IntWritable(1);

	@Override
	public void map(Object key, Text value, Context context)	throws IOException, InterruptedException {
		//从value中获取输入的数据，使用空格拆分输入数据，然后抽取出每一个单词
		//我们输入了一系列的键值对，键为拆分出来的一个个单词，值为常数1
		StringTokenizer itr = new StringTokenizer(value.toString());

		while (itr.hasMoreTokens()) {
            Text word = new Text();
			word.set(itr.nextToken());
			context.write(word, one);
		}
	}
}
